package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.pojo.*;
import com.wn.mapper.HotelHouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class HotelHouseService {
    @Autowired
    private HotelHouseMapper hotelHouseMapper;

    //查询所有
    public Rsp queryAll(PageParam<HotelHouse> pageParam){

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){

            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());

            List<HotelHouse> hotelHouseList = hotelHouseMapper.superSearch(pageParam.getSuperSearchKeyWord());

            PageInfo<HotelHouse> hotelHousePageInfo = new PageInfo<>(hotelHouseList);

            return Rsp.success(hotelHousePageInfo);

        }else {
            //分页
            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());

            List<HotelHouse> hotelHouseList = hotelHouseMapper.queryAll();

            PageInfo<HotelHouse> hotelHousePageInfo = new PageInfo<>(hotelHouseList);

            return Rsp.success(hotelHousePageInfo);
        }
    }
    //根据id查询
    public Rsp getHouseById(Integer id){
        HotelHouse hotelHouse = hotelHouseMapper.selectByPrimaryKey(id);
        if(StringUtils.isEmpty(hotelHouse)){
            return new Rsp().fail().msg("查询失败");
        }else{
            return new Rsp().success(hotelHouse);
        }
    }
    //增加数据
    public Rsp insertHouseType(HotelHouse hotelHouse){
        int insertSelective = hotelHouseMapper.insertSelective(hotelHouse);
        if(insertSelective == 1){
            return new Rsp().success(insertSelective);
        }else{
            return new Rsp().fail().msg("添加失败");
        }
    }
    //删除house类型
    public Rsp deleteByPrimaryKey(Integer id){
        int byPrimaryKey = hotelHouseMapper.deleteByPrimaryKey(id);
        if(byPrimaryKey == 1){
            return new Rsp().success(byPrimaryKey);
        }else {
            return new Rsp().fail().msg("删除失败！");

        }
    }

}
